<?php

namespace app\admin\controller\xmwms;

use app\admin\model\xmwms\OutboundOrderShipment;
use app\common\controller\Backend;

class OutboundOrderDetail extends Backend
{
    /**
     * @var \app\admin\model\xmwms\OutboundOrderDetail
     */
    protected object $model;

    protected object $shipmentModel;

    protected array $withJoinTable = ['wmsCarrier'];

    protected string|array $preExcludeFields = ['id', 'create_time', 'update_time'];

    protected string|array $quickSearchField = ['id'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model         = new \app\admin\model\xmwms\OutboundOrderDetail;
        $this->shipmentModel = new OutboundOrderShipment();
    }

    /**
     * 查看
     */
    public function index(): void
    {
        $this->request->filter(['strip_tags', 'trim']);
        // 如果是select则转发到select方法,若select未重写,其实还是继续执行index
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit) = $this->queryBuilder();
        $res = $this->model
            ->with(['outboundOrder' => function ($query) {
                $query->field('id,outbound_no,order_sn,customer_id,admin_id,type,audit_status,shipment_status,remark,create_time,update_time')
                    ->with(['admin'  => function ($queryAdmin) {
                        $queryAdmin->field('id,username');
                    }, 'wmsCustomer' => function ($querySupplier) {
                        $querySupplier->field('id,name,city,address');
                    }
                    ]);
            }, 'product'            => function ($queryProduct) {
                $queryProduct->field('id,product_no,bar_code,name,brand_id,unit_id,spec')
                    ->with(['wmsUnit' => function ($queryUnit) {
                        $queryUnit->field('id,name');
                    }
                    ]);
            }
            ])
            ->alias($alias)
            ->where($where)
            ->order('update_time asc')
            ->paginate($limit);
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }


    public function shipment(): void
    {
        $this->request->filter(['strip_tags', 'trim']);
        // 如果是select则转发到select方法,若select未重写,其实还是继续执行index
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->shipmentModel
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->with(['outboundOrder' => function ($query) {
                $query->field('id,outbound_no,order_sn,customer_id,admin_id,type,audit_status,shipment_status,remark,create_time,update_time')
                    ->with(['admin' => function ($queryAdmin) {
                        $queryAdmin->field('id,username');
                    }
                    ]);
            }])
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $res->visible(['wmsCarrier' => ['id', 'name', 'address', 'city', 'contact_person', 'mobile']]);
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

}